﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RoomSelector : MonoBehaviour {
	
	NetworkScript network;
	DataScript data;
	
	public GameObject panelPrefab;
	public GameObject joinButPref;
	public GameObject fullButPref;
	public List<UISprite> roomPanels;
	public List<Transform> roomPanelBut;
	private float timer = 0;
	float refreshFrequency = 8.0f;
	public List<Texture2D> roomPaneTex;
	public lobbyInfoScript lobbyInfo;
	public UIPanel headPanel;
	
	// Use this for initialization
	void Start () {
		roomPaneTex = new List<Texture2D>();
		for(int i = 1;;i++)
		{
			Texture2D tmpTex = Resources.Load("UI/icon_roomcreator0" + i.ToString()) as Texture2D;
			if(tmpTex == null)
				break;
			roomPaneTex.Add(tmpTex);
		}
		
		/*string com = "Play With ME!_Miguel_WORLD 1 - Level 1_1";
		string com2 = "Play With MEss!_Miguel_WORLD 1 - Level 2_3";
		
		for(int i = 0; i < 30;i++)
		{
			UISprite tmpObj = (GameObject.Instantiate(panelPrefab) as GameObject).GetComponent<UISprite>();
			tmpObj.transform.parent = this.transform;
			Vector3 tmpPos = new Vector3(-90, 130 - i * 100,2);
			tmpObj.transform.localPosition = tmpPos;
			tmpObj.transform.localScale = new Vector3(1,1,1);
			
			lobbyRoomPScropt tmpScript = tmpObj.GetComponent<lobbyRoomPScropt>();
			tmpScript.parentPanel = this;
			if(i < 4)
				tmpScript.InitPanel(com);
			else
				tmpScript.InitPanel(com2);
			
			roomPanels.Add(tmpObj);
			
		}
		
		for(int i = 0; i < 30;i++)
		{
			joinButtonScript tmpBut = (GameObject.Instantiate(joinButPref) as GameObject).GetComponent<joinButtonScript>();
			tmpBut.transform.parent = this.transform;
			Vector3 tmpPos = new Vector3(260, 130 - i * 100,2);
			tmpBut.transform.localPosition = tmpPos;
			tmpBut.transform.localScale = new Vector3(1,1,1);
			
			roomPanelBut.Add(tmpBut);
		}*/
		
		data = GameObject.FindGameObjectWithTag("DataManager").GetComponent<DataScript>();
		network = GameObject.FindGameObjectWithTag("NetworkManager").GetComponent<NetworkScript>();
		timer = refreshFrequency;
	}
	
	void Update () {
		//planes[0].updatePlane(true);

		if(timer > refreshFrequency)
		{
			//Debug.Log("REFRESH");
			timer = 0;
			network.refreshList();
		}
		else
		{
			timer += Time.deltaTime;
		}
		
	}
	
	public void OnToggleChange()
	{
		//UIToggle.current
	}
	
	void refreshHost(HostData[] hostData)
	{
		for(int i = 0; i < roomPanels.Count; i++)
			Destroy(roomPanels[i].gameObject);
		roomPanels.Clear();
		for(int i = 0; i < roomPanelBut.Count; i++)
			Destroy(roomPanelBut[i].gameObject);
		roomPanelBut.Clear();
		
		for(int i = 0; i < hostData.Length;i++)
		{
			UISprite tmpObj = (GameObject.Instantiate(panelPrefab) as GameObject).GetComponent<UISprite>();
			tmpObj.transform.parent = this.transform;
			Vector3 tmpPos = new Vector3(-90, 130 - i * 100,2);
			tmpObj.transform.localPosition = tmpPos;
			tmpObj.transform.localScale = new Vector3(1,1,1);
			
			lobbyRoomPScropt tmpScript = tmpObj.GetComponent<lobbyRoomPScropt>();
			tmpScript.parentPanel = this;
			tmpScript.InitPanel(hostData[i].comment);

			
			roomPanels.Add(tmpObj);
			
		}
		
		for(int i = 0; i < hostData.Length;i++)
		{
			Transform tmpBut;
			if(hostData[i].connectedPlayers == 1)
			{
				tmpBut = (GameObject.Instantiate(joinButPref) as GameObject).transform;
				tmpBut.GetComponent<joinButtonScript>().giveData(hostData[i]);
			}
			else
				tmpBut = (GameObject.Instantiate(fullButPref) as GameObject).transform;
			tmpBut.parent = this.transform;
			Vector3 tmpPos = new Vector3(260, 130 - i * 100,2);
			tmpBut.localPosition = tmpPos;
			tmpBut.localScale = new Vector3(1,1,1);
			
			roomPanelBut.Add(tmpBut);
		}
		//headPanel.Refresh();
	}
	
	void OnMasterServerEvent(MasterServerEvent mse)
	{
		if (mse == MasterServerEvent.HostListReceived)
		{
			//Debug.Log("Host List Refreshed");
			//MasterServer.ClearHostList();
			refreshHost(MasterServer.PollHostList());
			MasterServer.ClearHostList();
		}
	}
}
